#include "HotKeyManager.h"

HotKeyMap HotKeyManager::_HotKeys;

HotKeyManager& HotKeyManager::GetHotKeyManager() {
	
	static HotKeyManager instance;

	return instance;
}

void HotKeyManager::RegisterHotKey(int vKey, bool& state) {
	
	_HotKeys.insert(std::pair<int, bool&>(vKey, state));
}

void HotKeyManager::UnregisterHotKey(int vKey) {
	
	_HotKeys.erase(vKey);
}

void HotKeyManager::UnregisterHotKey(bool& state) {

	if (_HotKeys.size() == 0) return;

	HotKeyMap::iterator it;
	for (it = _HotKeys.begin(); it != _HotKeys.end(); ++it) {

		if (&(it->second) == &state) {
			
			_HotKeys.erase(it->first);
		}
	}
}

void HotKeyManager::UpdateHotKeyState(int msgCode) {

	if (_HotKeys.size() == 0) return;
	
	HotKeyMap::iterator it;
	for (it = _HotKeys.begin(); it != _HotKeys.end(); ++it) {
		
		if (it->first == msgCode) {
			
			it->second = !(it->second);
		}
	}
}

void HotKeyManager::KeyboardHandler(MSG msg) {
	
	if (msg.message == WM_KEYDOWN) {
		
		UpdateHotKeyState(msg.wParam);
	}
}

void HotKeyManager::KeyboardHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

	if (uMsg == WM_KEYDOWN) {

		UpdateHotKeyState(wParam);
	}
}
